package com.myz.service.ribbon.service.impl;

import com.myz.service.ribbon.service.IRibbonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @author maoyz
 */
@Service
public class RibbonServiceimpl implements IRibbonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RibbonServiceimpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
     * service名称要保持一致
     *
     * @return
     */
    @Override
    public String rest() {
        ParameterizedTypeReference<String> type = new ParameterizedTypeReference<String>() {
        };
        ResponseEntity<String> responseEntity = restTemplate.exchange("http://SERVICE-MEMBER/member/index", HttpMethod.GET, null, type);
        LOGGER.info("************** responseEntity = {} ***************", responseEntity);
        return responseEntity.getBody();
    }

    @Override
    public String balance() {
        // choose serviceName
        ServiceInstance instance = loadBalancerClient.choose("SERVICE-MEMBER");
        LOGGER.info("************** balance instance = {} : {} ****************", instance.getHost(), instance.getPort());
        URI uri = URI.create(String.format("http://%s:%s%s", instance.getHost(), instance.getPort(), "/member/index"));

        ParameterizedTypeReference<String> type = new ParameterizedTypeReference<String>() {
        };

        // 直接new的，无法使用service_name调用服务
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, type);
        LOGGER.info("************** responseEntity = {} ***************", responseEntity);
        return responseEntity.getBody();
    }

    /**
     * todo post 请求无重试????
     *
     * @param username
     * @return
     */
    @Override
    public String post(String username) {
        ParameterizedTypeReference<String> type = new ParameterizedTypeReference<String>() {
        };

        HttpHeaders headers = new HttpHeaders();
        //  请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Authorization", "secret");
        //  封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();

        params.add("username", username);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange("http://SERVICE-MEMBER/member/header", HttpMethod.POST, requestEntity, type);
        LOGGER.info("************** responseEntity = {} ***************", responseEntity);
        return responseEntity.getBody();
    }
}
